package com.kdtech.analyse.app;
import java.util.HashMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.StringUtils;

/**
 * @链接 http://app.news.baidu.com/
 * @名字 百度新闻APP
 * @作者 dhy
 */
public class BaiduAppAnalyse implements AnalyseNews {
	
	public static HashMap<String, String> getLinks(String entryUrl){
		HashMap<String, String> links = new HashMap<String, String>();
		
		UrlMeta meta = null;
		if(meta==null || StringUtils.isBlank(meta.getHtml())){
			return links;
		}
		
		String html = meta.getHtml();
		
		JSONObject jObj = JSONObject.parseObject(html);
		JSONObject data = jObj.getJSONObject("data");
		JSONArray news = data.getJSONArray("news");
		for(int i = 0; i < news.size(); i++){
			JSONObject anNew = news.getJSONObject(i);
			String url = anNew.getString("url");
			
			//如果是百度新闻的链接，则拼接真正的链接
			if(StringUtils.isNotBlank(url) || url.startsWith("http://news.baidu.com/")){
//				String article_id = URLUtil.getValue(url, "article_id");
				String article_id = null;
				if(StringUtils.isNotBlank(article_id) && article_id.matches("[0-9]+")){
					url = "http://news.baidu.com/n?m=app&v=focusservicedetail&tnp=json&article_id=" + article_id;
				}
			}
			links.put(url, "");
		}
		return links;
	}

	private static final String[] regex={
		"http://news.baidu.com/n\\?article_id=[0-9]+&m=app&tnp=json.*",
	};


	
	public boolean isDetailPage(String url) {
		for (int i=0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		//http://news.baidu.com/n?article_id=1451469925938&class=detail&cmd=2
		//http://news.baidu.com/n?m=app&v=focusservicedetail&tnp=json&article_id=1451469925938
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url=urlMeta.getUrl();
		news.setUrl(url);
		
//		String article_id = URLUtil.getValue(url, "article_id");
		String article_id = null;
		if(StringUtils.isNotBlank(article_id) && article_id.matches("[0-9]+")){
			String metaUrl = "http://news.baidu.com/n?article_id="+article_id+"&class=detail&cmd=2";
			news.setUrl(metaUrl);
		}
		
		String htmltxt=urlMeta.getHtml();
		String title="";
		String content="";
		String author="";
		Long date=null;
		
		try{
			JSONObject jObj = JSONObject.parseObject(htmltxt);
			JSONObject data = jObj.getJSONObject("data");
			JSONObject jsonNews = data.getJSONArray("news").getJSONObject(0);
			
			
			title = jsonNews.getString("title");
			author = jsonNews.getString("site");
			JSONArray contentList = jsonNews.getJSONArray("content");
			for(int i = 0; i < contentList.size(); i++){
				JSONObject jsonContent = contentList.getJSONObject(i);
				content += "<p>" + jsonContent.getString("data") + "</p>";
			}
		}catch(RuntimeException e){
			e.printStackTrace();
		}
		
		news.setTitle(StringUtils.trimSpace(title));
		news.setAuthor(author);
		news.setContent(content);
		news.setDate(date);

		return news;

	}

	

	

	
}

